﻿/* 
 * Zune Lyrics - A Zune Plugin to display the lyrics of track currently 
 * being played in the Zune Software
 * Copyright (C) 2009 - Sanrag Sood
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */



using System;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;
using RegistryUtils;

namespace ZuneLyrics
{
	public partial class Window1
	{

		private delegate void UpdateWindowDelegate(Song song);

		public Window1()
		{
			this.InitializeComponent();
			// Zune Now Playing contains check to make sure that only one instance of it
			// is running at any particular moment. Do NOT exit ZuneNowPlaying when exiting
			// ZuneLyrics, it might be needed by other processes
			try
			{
				Process.Start("ZuneNowPlaying.exe");
			}
			catch (Exception)
			{
				lyrics.Text = "Can't start ZuneNowPlaying.exe";
			}
			System.Net.ServicePointManager.Expect100Continue = false;
			RegistryMonitor monitor = new RegistryMonitor(@"HKCU\Software\ZuneNowPlaying");
			monitor.RegChanged += new EventHandler(OnRegChange);
			monitor.Start();
			Thread t = new Thread(new ThreadStart(ZuneLyricsWindow.UpdateLyrics));
			t.Start();
		}

		private void OnRegChange(Object obj, EventArgs e)
		{
			Thread t = new Thread(new ThreadStart(ZuneLyricsWindow.UpdateLyrics));
			t.Start();
		}

		public void UpdateLyrics()
		{
			RegistryKey regkey;
			String title, artist, lyrics;
			int status;
			try
			{
				regkey = Registry.CurrentUser.OpenSubKey(@"Software\ZuneNowPlaying");
				status = (int)regkey.GetValue("Playing");
				// Do nothing if no track is being played
				if (status == 0)
				{
					regkey.Close();
					return;
				}
				title = regkey.GetValue("Title") as string;
				artist = regkey.GetValue("Artist") as string;
				lyrics = LyricsFetcher.GetLyrics(artist, title);
				ZuneLyricsWindow.Dispatcher.Invoke(new UpdateWindowDelegate(UpdateWindow), new Song(artist, title, lyrics));
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}

		}

		public void UpdateWindow(Song song)
		{
			String title, artist;
			// This check is needed if the user switches songs too fast
			RegistryKey regkey;
			regkey = Registry.CurrentUser.OpenSubKey(@"Software\ZuneNowPlaying");
			int status = (int)regkey.GetValue("Playing");
			// Do nothing if no track is being played
			if (status == 0)
			{
				regkey.Close();
				return;
			}
			title = regkey.GetValue("Title") as string;
			artist = regkey.GetValue("Artist") as string;

			if (title != song.Title || artist != song.Artist)
				return;

			this.title.Content = song.Title;
			this.artist.Content = song.Artist;
			this.lyrics.Text = song.Lyrics;
			this.lyrics.ScrollToHome();
			return;
		}

		private void KeyPressed(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if (e.Key == Key.F5)
			{
				Thread t = new Thread(new ThreadStart(ZuneLyricsWindow.UpdateLyrics));
				t.Start();
			}
		}

	}
}